var $ = require('../../lib/jquery');
var util = require('./common/util');
var falg = true,
    countdown = 180,
    passWord;

$('.js_login').on('click', function () {
    window.location.href = './login.html'
})
$('.js_code').on('click', function () {
    var login = $('.js_phone').val();
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(login)) {
        alert('手机号有误')
    } else {
        if (falg) {
            falg = false;
            util.fetch('login/user/SendMessage?type=0&fun=1&phoneNum=' + login).then(function (res) {
                if (res.code == 'USR000') {
                    settime();
                    sessionStorage.setItem('login', login);
                }
            }).fail(function (res) {
                falg = true;
                alert(res.msg)
            })
        }

    }

})

function settime() {
    if (countdown == 0) {
        falg = true;
        $('.js_code').html('获取验证码');
        $('.js_code').removeClass('js_code1');
        countdown = 180;
    } else {
        countdown--;
        $('.js_code').html(countdown + 's');
        $('.js_code').addClass('js_code1');

        setTimeout(function () {
            settime()
        }, 1000)
    }

}

$('.js_first').on('click', function () {
    var login = $('.js_phone').val();
    var code = $('.code').val();
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (login == '') {
        alert('请填写手机号')
    } else if (!myreg.test(login)) {
        alert('手机号有误')
    } else if (code == '') {
        alert('请填写验证码')
    } else {
        util.fetch('login/user/validatCode?fun=1&login=' + login + '&code=' + code).then(function (res) {
            sessionStorage.setItem('login', login);
            sessionStorage.setItem('code', code);
            $('.first').addClass('hide');
            $('.second').removeClass('hide');

        }).fail(function (res) {
            alert(res.msg)
            countdown = 0;
        })

    }

})
$('.js_second').on('click', function () {
    var userName = ($('.js_userName').val() || '').replace(/s+/g, '');
    var idCard = $('.js_idCard').val();
    var regname = /^[\u4E00-\u9FA5]{2,5}$/i;
    var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
    if (userName == '') {
        alert("姓名不能为空");
    } else if (!regname.test(userName)) {
        alert('姓名输入不合法')
    } else if (!reg.test(idCard)) {
        alert("身份证输入不合法");
    } else {
        util.fetch('login/user/idCardVerify?idNumber=' + idCard + '&name=' + userName).then(function (res) {
            if (JSON.parse(res.data).ret == '1') {
                sessionStorage.setItem('userName', userName);
                sessionStorage.setItem('idCard', idCard);
                $('.second').addClass('hide');
                $('.three').removeClass('hide');
                $('.js_login').hide();
                $('.js_login').hide();
            } else {
                alert('姓名与身份证不匹配')
            }

        }).fail(function (res) {
            alert(JSON.parse(data.data).msg)
        })
    }

})
$('.js_zheng_image').change(function (e) {
    $('.js_message').removeClass('hide');
    var f = new FormData();
    if (this.files[0].size < 614400) {
        var url = URL.createObjectURL(this.files[0]);
        this.file = this.files[0];
        f.append('idcard_poto_f', this.file);
        $.ajax({
             url: 'https://app.jkxch.bjxch.gov.cn/login/user/uploadPhoto',
            //http: "https://117.121.38.74:8089/login/user/uploadPhoto",
            data: f,
            type: "POST",
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.code == 'USR000') {
                    sessionStorage.setItem('idcard_poto_f', res.data.idcard_poto_f);
                    $('.zheng').attr('src', res.data.idcard_poto_f);
                } else {
                    alert(res.msg)
                }
                $('.js_message').addClass('hide');
            }
        })
    } else {
        $('.js_message').addClass('hide');

        alert('上传图片不能超过600K')
    }
})
$('.js_fan_image').change(function (e) {
    var f = new FormData();
   
    if (this.files[0].size < 614400) {
        var url = URL.createObjectURL(this.files[0]);
        this.file = this.files[0];
        f.append('idcard_poto_b', this.file);
        $('.js_message').removeClass('hide');
        $.ajax({
            // url: 'http://117.121.38.74:8089/login/user/uploadPhoto',
           url: "https://app.jkxch.bjxch.gov.cn/login/user/uploadPhoto",
            data: f,
            type: "POST",
            processData: false,
            contentType: false,
            success: function (res) {

                if (res.code == 'USR000') {
                    sessionStorage.setItem('idcard_poto_b', res.data.idcard_poto_b)
                    $('.fan').attr('src', res.data.idcard_poto_b);
                } else {

                    alert(res.msg)
                }
                $('.js_message').addClass('hide');
            }
        })
    } else {
        $('.js_message').addClass('hide');
        alert('上传图片不能超过600K')
    }
})
$('.js_three').on('click', function () {
    if (sessionStorage.getItem('idcard_poto_f') && sessionStorage.getItem('idcard_poto_b')) {
        $('.three').addClass('hide');
        $('.five').removeClass('hide');

    } else {
        alert('请上传身份证')
    }
})
$('.js_four').on('click', function () {
    $('.js_message').removeClass('hide');
    $('.four').addClass('hide');
    $('.five').removeClass('hide');
    util.fetch('login/user/handleUserLogin?login=' + sessionStorage.getItem('login') + '&passWord=' + passWord).then(function (res) {
        $('.js_message').addClass('hide');
        sessionStorage.setItem('token', res.data.token);
        sessionStorage.setItem('userId', res.data.userId);
        window.location.href = './index.html';

    }).fail(function (res) {
        $('.js_message').addClass('hide');
        alert(res.msg)
    })
})
$('.js_avator').change(function (e) {
    $('.js_message').removeClass('hide');
    var f = new FormData();
    var url = URL.createObjectURL(this.files[0]);
    if (this.files[0].size < 614400) {
        this.file = this.files[0];
        f.append('photoUrl', this.file);
        $('.fivePic').css({
            'backgroundImage': 'url(' + url + ')',
            'background-size': 'covr'
        });
        $.ajax({
            // url:'http://117.121.38.74:8089/login/user/uploadPhoto',
            url: 'https://app.jkxch.bjxch.gov.cn/login/user/uploadPhoto',
            data: f,
            type: "POST",   
            processData: false,
            contentType: false,
            success: function (res) {
                $('.js_message').addClass('hide');
                if (res.code == 'USR000') {
                    sessionStorage.setItem('photoUrl', res.data.photoUrl)
                } else {
                    $('.js_message').addClass('hide');
                    alert(res.msg)
                }
            }
        })
    } else {
        alert('上传图片不能超过600K')
    }

})

$('.js_five').on('click', function () {
    passWord = $('.js_word').val();
    var reg = /((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))(?!^.*[\u4E00-\u9FA5].*$)^\S{4,16}$/;
    if (passWord == '') {
        alert('请填写密码')
    } else if (passWord != $('.js_word1').val()) {
        alert('两次密码不一样')
    } else if (!reg.test(passWord)) {
        alert('字母，数字，符号，至少两种')
    } else {
        util.fetch('login/user/handleCreateUser', {
            login: sessionStorage.getItem('login'),
            passWord: passWord,
            code: sessionStorage.getItem('code'),
            idCard: sessionStorage.getItem('idCard'),
            userName: sessionStorage.getItem('userName'),
            photoUrl: sessionStorage.getItem('photoUrl'),
            idcard_poto_f: sessionStorage.getItem('idcard_poto_f'),
            idcard_poto_b: sessionStorage.getItem('idcard_poto_b'),
        }).then(function (res) {
            $('.four').removeClass('hide');
            $('.five').addClass('hide');

            sessionStorage.removeItem('userName');
            sessionStorage.removeItem('photoUrl');
            sessionStorage.removeItem('idCard');
            sessionStorage.removeItem('idcard_poto_f');
            sessionStorage.removeItem('idcard_poto_b');
            sessionStorage.removeItem('idCard');
            sessionStorage.removeItem('code');



        }).fail(function (res) {
            window.location.href = './registe.html';
            alert(res.msg);
        })
    }

})
$('.js_a2').on('click', function () {
    location.href = 'http://223.71.187.80:9010/bmxt';
})